<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.toolName" placeholder="Tool Name" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <DictGroupInput ref="dictGroupInput" :value.sync="listQuery.category" dict-group="tool_category" placeholder="Category" class="filter-item" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">Search</el-button>
      <!-- show column popover -->
      <el-popover v-if="showList" placement="bottom" width="150" trigger="click">
        <el-checkbox v-model="showRows.category" class="filter-item" @change="tableKey = tableKey + 1">Category</el-checkbox>
        <el-checkbox v-model="showRows.turnTo" class="filter-item" @change="tableKey = tableKey + 1">Turn To</el-checkbox>
        <el-checkbox v-model="showRows.icon" class="filter-item" @change="tableKey = tableKey + 1">Icon</el-checkbox>
        <el-checkbox v-model="showRows.standby" class="filter-item" @change="tableKey = tableKey + 1">Standby</el-checkbox>
        <el-checkbox v-model="showRows.state" class="filter-item" @change="tableKey = tableKey + 1">State</el-checkbox>

        <el-button slot="reference" v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-setting" />
      </el-popover>
      <router-link :to="{name:'CreateOnlineTool'}">
        <el-button v-waves class="filter-item" type="primary" plain icon="el-icon-plus" />
      </router-link>

      <svg-icon v-if="showList" icon-class="component" style="float:right; color: grey;" @click="showList=false" />
      <svg-icon v-if="!showList" icon-class="list" style="float:right; color: grey;" @click="showList=true" />
    </div>

    <!-- <el-row v-show="!showList" v-loading="listLoading" :gutter="40" class="panel">
      <el-col v-for="row in list" :key="row.id" :xs="8" :sm="4" :lg="3" class="panel-col">
        <el-row>
          <el-link :href="row.turnTo" :underline="false" target="_blank">
            <el-tooltip effect="dark" :content="row.category" placement="top">
              <el-avatar shape="circle" :size="100" fit="cover" :src="row.icon" style="box-shadow:4px 4px 40px rgba(158, 5, 5, 0.5);" />
            </el-tooltip>
          </el-link>
        </el-row>
        <el-row>
          <span class="title">{{ row.toolName }}</span>
        </el-row>
      </el-col>
    </el-row>-->
    <el-row v-show="!showList" v-loading="listLoading" :gutter="40">
      <el-col v-for="(category,index) in categories" :key="category.id" :xs="{span: 24}" :sm="{span: 12}" :md="{span: 24}" :lg="{span: 8}" :xl="{span: 8}" style="margin-bottom:30px;">
        <el-card shadow="hover" class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ category.text }}</span>
          </div>
          <el-table :data="classifiedList[index]" max-height="300" :show-header="false" stripe style="width: 100%">
            <el-table-column align="center" width="30" type="index" />
            <el-table-column align="right" width="30" prop="toolName" label="ToolName">
              <template slot-scope="scope">
                <el-avatar :size="20" :src="scope.row.icon">
                  <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png">
                </el-avatar>
              </template>
            </el-table-column>
            <el-table-column prop="toolName" label="ToolName">
              <template slot-scope="scope">
                <el-link :href="scope.row.turnTo" type="primary" target="_blank">{{ scope.row.toolName }}</el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-table v-show="showList" :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" prop="id" label="ID" width="80" />
      <el-table-column align="center" prop="toolName" label="Tool Name" />
      <el-table-column v-if="showRows.category" align="center" prop="category" label="Category" />

      <el-table-column v-if="showRows.turnTo" label="Turn To" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="scope.row.turnTo" type="primary" target="_blank">{{ scope.row.turnTo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column v-if="showRows.icon" label="Icon" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="scope.row.icon" type="primary" target="_blank">{{ scope.row.icon }}</el-link>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.standby" align="center" label="Standby" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="scope.row.standby" type="primary" target="_blank">{{ scope.row.standby }}</el-link>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.state" align="center" prop="state" label="State" show-overflow-tooltip />

      <el-table-column width="180px" align="center" prop="modify_at" sortable="custom" label="UpdateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.modifyAt | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="Actions" width="120">
        <template slot-scope="scope">
          <router-link :to="{name: 'EditOnlineTool', params: scope.row }">
            <el-button type="primary" size="mini" icon="el-icon-edit" plain />
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="handleDelete(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" :page-sizes="pageSizes" @pagination="getList" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { fetchList, deleteAccountPassword } from '@/api/util/online-tool'
import { getSorts } from '@/utils/index'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import DictGroupInput from '@/views/components/DictGroupInput'

export default {
  name: 'OnlineToolList',
  components: { DictGroupInput, Pagination },
  directives: { waves },
  data() {
    return {
      showList: false,
      tableKey: 0,
      showRows: {
        turnTo: true,
        standby: true,
        state: true,
        category: true,
        icon: true
      },
      list: null,
      classified: [],
      classifiedList: {},
      total: 0,
      sort: {},
      listLoading: true,
      listQuery: {
        uid: undefined,
        toolName: undefined,
        category: undefined,
        current: 1,
        size: 100,
        asc: undefined,
        desc: undefined
      },
      pageSizes: [100, 200, 300, 500]
    }
  },
  computed: {
    ...mapGetters(['uid', 'dictionaries']),
    categories() {
      return this.dictionaries.filter((category) => category.dictGroup === 'tool_category')
    }
  },
  created() {
    this.listQuery.uid = this.uid
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery)
        .then((response) => {
          this.list = response.result.list
          this.total = response.result.total
          this.categories.forEach((v, i) => {
            this.classifiedList[i] = this.getClassifiedList(v.text)
          })
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    // 获取{category}分类的工具列表
    getClassifiedList(category) {
      return this.list.filter((tool) => tool.category === category) || []
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.sort[prop] = undefined
      if (order) {
        this.sort[prop] = order !== 'descending'
      }
      const sort = getSorts(this.sort)
      this.listQuery.asc = sort.asc
      this.listQuery.desc = sort.desc
      this.handleFilter()
    },
    handleDelete(id) {
      this.$confirm('Confirm delete record?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        deleteAccountPassword(id).then((response) => {
          console.log(response)
          this.$message({
            type: 'success',
            message: 'deleted successfully!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}

.panel {
  padding: 5px 20px 5px 20px;
  .panel-col {
    text-align: center;
    margin-bottom: 26px;
  }
  .title {
    color: grey;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
  }
}
</style>
